// pages/movie/movie.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 热门电影数据
    inTheatersList: [],
    // 即将上映数据
    comingsoonList: [],
    // top250数据
    topList: [],
    // 加载更多
    showLoading: false,
    // 搜索时的切换
    isSearching: false,
    // 搜索的数据
    searchList: [],
    // 当搜索不到信息时
    isSearchEmpty: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getIntheatersList()
    this.getcomingsoonList()
    this.getTopList()
  },
  /* 上拉加载更多 */
  onReachBottom (e) {
    this.setData({
      showLoading: true
    })
    this.getTopList()
  },
  // 获取电影数据
  getIntheatersList () {
    const This = this
    wx.request({
      url: 'http://api.douban.com/v2/movie/in_theaters?apikey=0df993c66c0c636e29ecbb5344252a4a',
      data:{
        count: 5
      },
      header:{
        "Content-Type":"json"
      },
      success (data) {
        This.setData({
          inTheatersList: data.data.subjects
        })
      },
      fail (data) {
        // 请求失败，弹出框提示
        wx.showToast({
          title: '网络请求失败',
          icon: 'none'
        })
      }
    })
  },
  // 获取即将上映的数据
  getcomingsoonList () {
    const This = this
    wx.request({
      url: 'http://api.douban.com/v2/movie/coming_soon?apikey=0df993c66c0c636e29ecbb5344252a4a',
      data:{
        count: 3
      },
      header:{
        "Content-Type":"json"
      },
      success (data) {
        console.log(data)
        This.setData({
          comingsoonList: data.data.subjects
        })
        console.log(This.data.comingsoonList)
      },
      fail (data) {
        // 请求失败，弹出框提示
        wx.showToast({
          title: '网络请求失败',
          icon: 'none'
        })
      }
    })
  },
  // 获取即将上映的数据
  getTopList () {
    const This = this
    const start = this.data.topList.length
    wx.request({
      url: 'http://api.douban.com/v2/movie/top250?apikey=0df993c66c0c636e29ecbb5344252a4a',
      data:{
        start: start,
        count: 9
      },
      header:{
        "Content-Type":"json"
      },
      success (data) {
        var tempList = This.data.topList;
        for (var i = 0;i<data.data.subjects.length;i++){
          tempList.push(data.data.subjects[i])
        }
        This.setData({
          topList: tempList,
          showLoading: false
        })
        console.log(This.data.topList)
      },
      fail (data) {
        // 请求失败，弹出框提示
        wx.showToast({
          title: '网络请求失败',
          icon: 'none'
        })
      }
    })
  },
  // 获取搜索的信息
  getSearchList (queryValue) {
    const This = this
    wx.request({
      url: 'https://api.douban.com/v2/movie/search?apikey=0df993c66c0c636e29ecbb5344252a4a',
      data:{
        q: queryValue
      },
      header:{
        "Content-Type":"json"
      },
      success (data) {
        var searchlist = data.data.subjects
        if (searchlist.length) {
          var isSearchEmpty = true
        } else {
          var isSearchEmpty = false
        }
        console.log(data)
        This.setData({
          searchList: data.data.subjects,
          isSearching:  true,
          isSearchEmpty: isSearchEmpty
        })
        console.log(This.data.comingsoonList)
      },
      fail (data) {
        // 请求失败，弹出框提示
        wx.showToast({
          title: '网络请求失败',
          icon: 'none'
        })
      }
    })
  },
  // 触碰事件显示跳转movie-detail页面
  onOpendetail (e) {
    const movieId = e.currentTarget.dataset.movieid
    wx.navigateTo({
      url: '../movie-detail/movie-detail?movieId=' + movieId,
    })
  },
  // 点击跳转movie-more页面
  onOpenMore (e) {
    wx.navigateTo({
      url: '../movie-more/movie-more',
    })
  },
  // 当找不到图片的时候，触发binderror事件
  onErrorImage (e) {
    const index = e.currentTarget.dataset.index
    const topList = this.data.topList
    const movieItem = topList[index]
    movieItem.images.small = '../../images/error_img.jpg'
    topList[index] =  movieItem
    this.setData({
      topList: topList
    })
  },
  // 搜索栏获取值
  onSearch (e) {
    if (e.detail.value){
      this.getSearchList(e.detail.value)
    }else{
      this.setData({
        isSearching: false
      })
    }
  }
})